package com.xunji.user.util;

import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;

/**
 * @program: ins-trial-manager
 * @ClassName BytesUtil
 * @description:
 * @author: xuqiang_imyours
 * @create: 2025-02-06 15:11
 * @Version 1.0.0
 **/
@Slf4j
public class BytesUtil {

    /**
     * 等量切分数组
     *
     * @param array
     * @param ch
     * @return
     */
    public static ArrayList<byte[]> splitByteArray(byte[] array, int ch) {
        if (ch <= 0 || array == null || array.length == 0) {
            return new ArrayList<>();
        }

        int partSize = array.length / ch;
        int remainder = array.length % ch;

        ArrayList<byte[]> result = new ArrayList<>();
        int startIndex = 0;

        for (int i = 0; i < ch; i++) {
            int size = partSize + (i < remainder ? 1 : 0); // 处理余数部分
            byte[] part = new byte[size];
            System.arraycopy(array, startIndex, part, 0, size);
            result.add(part);
            startIndex += size;
        }
        //log.info("切分后的数组数量为：{}，每个数组的长度为：{}", result.size(), result.get(0).length);
        return result;
    }

    public static void main(String[] args) {
        byte[] byteArray = {0, 1, 2, 3, 4, 5, 6, 7, 8};
        ArrayList<byte[]> result = splitByteArray(byteArray, 2);
        System.out.println("切分后的数组长度：" + result.size());
        System.out.println(JSONObject.toJSONString(result));
        System.out.println(result.get(0).length);
    }
}
